package com.gxa.global.realm;

import com.gxa.entity.CitySysUser;
import com.gxa.mapper.CitySysUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class CityRealm extends AuthorizingRealm {

    @Autowired
    private CitySysUserMapper citySysUserMapper;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("====进入认证方法====");
        //获取客户端传递的账号和密码
        String userName = String.valueOf(authenticationToken.getPrincipal());

        CitySysUser citySysUser = citySysUserMapper.findSysUserByUsername(userName);

        System.out.println(citySysUser);
        if (citySysUser !=null){
            //将数据库中的账户和密码与客户端传递的账户密码进行比较
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(citySysUser.getUserName(),citySysUser.getPassword(),"cityRealm");
            //加盐值
            info.setCredentialsSalt(ByteSource.Util.bytes(citySysUser.getSalt()));

            return  info;
        }
        return null;
    }
}

